<template>
  <div class="flex flex-col h-full">
    <SearchContainer @search="queryList" @reset="() => (formState = {})">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :colon="false" :label-col="{ style: { width: '70px' } }">
        <a-form-item name="type">
          <a-radio-group v-model:value="tabActiveKey" button-style="solid" class="w-96 flex mx-0" @change="TabRadioChange">
            <a-radio-button v-for="(item, index) in options" :key="index" :value="item.bh">{{ item.mc }}</a-radio-button>
          </a-radio-group>
        </a-form-item>
        <a-form-item name="zjm" class="ml-4">
          <a-input v-model:value="formState.zjm" class="w-64" placeholder="编码/名称/助记码" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <div class="flex mb-2">
        <a-button type="primary" ghost class="mr-2" @click="onChangeStatus('1')">批量启用</a-button>
        <a-button type="primary" ghost danger @click="onChangeStatus('0')">批量停用</a-button>
      </div>
      <BaseTable
        :loading="loading"
        :column-code="columnNum"
        row-key="id"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        fill
        :data-source="tableData"
        bordered
        :scroll="{ x: 2200 }"
        :pagination="{ total: page.total, current: page.current, pageSize: page.pageSize }"
        @change="onPageChange"
      >
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'dj'">
            {{ record.dj?.toFixed(3) }}
          </template>
          <template v-if="column.dataIndex === 'mc'">
            <MedicineInfo :value="{ mc: record?.mc, gg: record?.gg }" />
          </template>
          <template v-if="column.dataIndex === 'status'">
            <a-tag v-if="record.status === '1'" color="cyan">启用</a-tag>
            <a-tag v-else color="red">停用</a-tag>
            <!-- <a-switch v-model:checked="record.status" :checked-value="'1'" :un-checked-value="'0'" @change="onChangeStatus(record, $event)" /> -->
          </template>
          <template v-if="column.dataIndex === 'certificateTime'">
            <span v-if="record.certificateStartTime">{{ record.certificateStartTime }}~{{ record.certificateEndTime }}</span>
            <span v-else>-</span>
          </template>
          <template v-if="column.dataIndex === 'isImport'">
            <span v-if="record.isImport === '1'">是</span>
            <span v-else>否</span>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="toSkuPage(record)">SKU</a-button>
            <a-divider type="vertical" />
            <a-button type="link" size="small" @click="openPriceModal(record)">批量调价</a-button>
            <a-divider type="vertical" />
            <a-button type="link" size="small" @click="onPrint(record)">打印</a-button>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Price v-model:open="priceModal" :row-data="rowData" @ok="queryList" />
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { post } from '@/utils/request'
import SearchContainer from '@/components/SearchContainer'
import BaseTable from '@/components/BaseTable'
import Price from './Price.vue'
// 镜架 60  设备 64  其他商品 65

const router = useRouter()
const loading = ref(false)
const tableData = ref([])
const formRef = ref(null)
const formState = ref({})
const options = ref([])
const page = ref({ total: 0, current: 1, pageSize: getSystemConfig('DFFAULT_PAGE_SHOW_COUNT') || 10 })
const columnNum = ref('000221')
const tabActiveKey = ref(60)
const selectedRowKeys = ref([])
const priceModal = ref(false)
const rowData = ref({})

const getProjectList = async () => {
  const { code, msg, data } = await post('/optometry/ooadolescentcheck/getDictOrderCategoryListById', { ids: [60, 65, 64] })
  if (code !== 0) return message.error(msg)
  options.value = data
}

const onChangeStatus = async (status) => {
  if (selectedRowKeys.value.length <= 0) return message.error('请至少选择一项')
  const arr = []
  let obj = {}
  selectedRowKeys.value.forEach((item) => {
    obj = {}
    obj.id = item
    obj.status = status
    arr.push(obj)
  })
  const { code, msg } = await post('/operation/opex/doUpdateOoProductStatus', arr)
  code === 0 ? message.success('操作成功') : message.error(msg || '操作失败')
  selectedRowKeys.value = []
  queryList()
}

const queryList = async () => {
  loading.value = true
  const { code, data, total, msg } = await post('/operation/opex/getOoProductList', {
    splb: tabActiveKey.value,
    zjm: formState.value.zjm,
    pageNum: page.value.current,
    pageSize: page.value.pageSize
  })
  if (code !== 0) return message.error(msg)
  tableData.value = data
  page.value.total = total
  loading.value = false
}
const openPriceModal = (record) => {
  rowData.value = record
  priceModal.value = true
}
const toSkuPage = (record) => {
  router.push({
    path: '/setting/optometry/commodity/frame-detail',
    query: {
      id: record.id
    }
  })
}
const onPrint = async (record) => {
  const { code, data, msg } = await post('/operation/opex/printDictOoProductSku', {
    productId: record.id
  })
  if (code !== 0) return message.error(msg || '打印失败')
  window.openExe?.rpt(data)
}
const onPageChange = ({ current, pageSize }) => {
  page.value.current = current
  page.value.pageSize = pageSize
  queryList()
}
const onSelectChange = (val) => {
  selectedRowKeys.value = val
}
onMounted(() => {
  getProjectList()
  queryList()
})
const TabRadioChange = (e) => {
  if (e.target.value === 65) {
    //其它商品
    columnNum.value = '000223'
  } else if (e.target.value === 64) {
    //设备
    columnNum.value = '000222'
  } else {
    //镜架
    columnNum.value = '000221'
  }
  queryList()
}
</script>
<style lang="less" scoped>
:deep(.ant-radio-button-wrapper) {
  flex: 1;
  color: var(--primary-color);
  text-align: center;
  border-color: var(--primary-color);
  &::before {
    background-color: var(--primary-color);
  }
}
.ant-picker {
  width: 100%;
}
</style>
